/*
 * @Descripttion: 
 * @version: 
 * @Author: WenQingwu
 * @Date: 2022-01-14 15:37:34
 * @LastEditors: WenQingwu
 * @LastEditTime: 2022-03-11 16:41:55
 */

// 登录
$(".login-btn").click(function () {
  let uname = $("#UserName").val();
  let upwd = $("#inputPassword").val();
  let flag = true;
  if (uname.trim().length === 0) {
    $("#UserName").removeClass('is-valid')
    $("#UserName").addClass('is-invalid')
    flag = false;
  } else {
    $("#UserName").removeClass('is-invalid')
    $("#UserName").addClass('is-valid')
  }
  if (upwd.trim().length === 0) {
    $("#inputPassword").removeClass('is-valid')
    $("#inputPassword").addClass('is-invalid')
    flag = false;
  } else {
    $("#inputPassword").removeClass('is-invalid')
    $("#inputPassword").addClass('is-valid')
  }
  if (flag) {
    $.ajax({
      url: '/user/login',
      type: 'post',
      data: {
        username: uname, password: upwd
      }
    })
      .done(res => {
        console.log(res);
        // 做一个本地存储 ，在跳转到下一下页面时，从本地拿用户基本信息，更新DOM信息。
        if (res.code === 0) {

          sessionStorage.setItem('userInfo', JSON.stringify(res.data))
          // 增加token的本地存储 
          sessionStorage.setItem('token', res.token)

          setTimeout(function () {
            location.href = "/html/main.html"
          }, 500)
        } else {
          console.log('登录失败');
          $("#UserName").addClass('is-invalid')
          $("#inputPassword").addClass('is-invalid')

        }
      })
  }

})